using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public interface IPage
{
    public void OpenPage();
    public void ClosePage();
}


public class UIController : MonoBehaviour
{
    public static UIController instance;
    private void Awake()
    {
        instance = this;
    }

    [SerializeField] List<CanvasGroup> canvasGroupList;
    [SerializeField] List<Button> buttonList;

    private void Start()
    {
        for (int i = 0; i< buttonList.Count;i++)
        {
            buttonList[i].onClick.AddListener(PlayButtonSFX);
        }
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            OpenPage(0);
        }
    }

    private void PlayButtonSFX()
    {
        AudioManager.Instance.PlaySFX(0);
    }

    public void OpenPage(int index)
    {
        if (index >= canvasGroupList.Count || index < 0 || canvasGroupList.Count == 0) return;
        canvasGroupList[index].alpha = 1;
        canvasGroupList[index].interactable = true;
        canvasGroupList[index].blocksRaycasts = true;
        if (canvasGroupList[index].TryGetComponent<IPage>(out var page))
        {
            page.OpenPage();
        }
    }

    public void ClosePage(int index)
    {
        if (index >= canvasGroupList.Count || index < 0 || canvasGroupList.Count == 0) return;
        canvasGroupList[index].alpha = 0;
        canvasGroupList[index].interactable = false;
        canvasGroupList[index].blocksRaycasts = false;
        if(canvasGroupList[index].TryGetComponent<IPage>(out var page))
        {
            page.ClosePage();
        }
    }
}
